{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "svm模型训练完成，用时：6346.71秒\n",
      "Accuracy: 0.54\n",
      "svm模型评估完成，用时：50.80秒\n"
     ]
    }
   ],
   "source": [
    "import pickle  \n",
    "import numpy as np  \n",
    "import time\n",
    "from sklearn.model_selection import train_test_split  \n",
    "from sklearn.preprocessing import StandardScaler  \n",
    "from sklearn.svm import SVC  \n",
    "from sklearn.metrics import classification_report, confusion_matrix ,accuracy_score\n",
    "  \n",
    "# 加载Pickle文件  \n",
    "with open('X.pkl', 'rb') as file:  \n",
    "    x = pickle.load(file)  \n",
    "  \n",
    "with open('y.pkl', 'rb') as file:  \n",
    "    y = pickle.load(file)  \n",
    "  \n",
    "# 确保x和y的形状匹配  \n",
    "assert len(x) == len(y), \"The number of samples in x and y do not match.\"  \n",
    "\n",
    "# 划分数据集为训练集和测试集  \n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=42, stratify=y)  \n",
    "  \n",
    "# 标准化数据  \n",
    "scaler = StandardScaler()  \n",
    "X_train = scaler.fit_transform(x_train)  \n",
    "X_test = scaler.transform(x_test)  \n",
    "  \n",
    "# 创建并训练SVM模型  \n",
    "clf3= SVC(kernel='linear', C=1.0, random_state=42,probability=True)  # 可以尝试不同的核函数和C参数  \n",
    "\n",
    "# 记录训练开始时间\n",
    "start_time_train = time.time()\n",
    "\n",
    "#训练模型\n",
    "clf3.fit(X_train, y_train)  \n",
    "\n",
    "# 记录训练结束时间并计算用时\n",
    "end_time_train = time.time()\n",
    "train_duration = end_time_train - start_time_train\n",
    "print(f\"svm模型训练完成，用时：{train_duration:.2f}秒\")\n",
    "\n",
    "# 记录评估开始时间\n",
    "start_time_test = time.time()\n",
    "\n",
    "# 测试 \n",
    "y_pred = clf3.predict(X_test)  \n",
    "\n",
    "#print(classification_report(y_test, y_pred))  \n",
    "#print(confusion_matrix(y_test, y_pred))  \n",
    "\n",
    "\n",
    "# 计算并打印准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f'Accuracy: {accuracy:.2f}')\n",
    "\n",
    "# 记录评估结束时间并计算用时\n",
    "end_time_test = time.time()\n",
    "test_duration = end_time_test - start_time_test\n",
    "print(f\"svm模型评估完成，用时：{test_duration:.2f}秒\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle  \n",
    "import numpy as np  \n",
    "import time\n",
    "from sklearn.model_selection import train_test_split  \n",
    "from sklearn.preprocessing import StandardScaler  \n",
    "from sklearn.svm import SVC  \n",
    "from sklearn.metrics import classification_report, confusion_matrix ,accuracy_score\n",
    "from sklearn.model_selection import train_test_split \n",
    "import pickle\n",
    "with open('X.pkl', 'rb') as file:  \n",
    "    X = pickle.load(file)  \n",
    "  \n",
    "with open('y.pkl', 'rb') as file:  \n",
    "    y = pickle.load(file)  \n",
    " \n",
    "\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42,stratify=y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "random_forest模型训练完成，用时：39.22秒\n",
      "Accuracy: 0.64\n",
      "random_forest模型评估完成，用时：0.28秒\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "# 创建随机森林分类器  \n",
    "clf2 = RandomForestClassifier(n_estimators=100, random_state=42)  \n",
    "\n",
    "# 记录训练开始时间\n",
    "start_time_train = time.time()\n",
    "\n",
    "# 训练模型  \n",
    "clf2.fit(X_train, y_train)\n",
    "\n",
    "# 记录训练结束时间并计算用时\n",
    "end_time_train = time.time()\n",
    "train_duration = end_time_train - start_time_train\n",
    "print(f\"random_forest模型训练完成，用时：{train_duration:.2f}秒\")\n",
    "\n",
    "# 记录训练开始时间\n",
    "start_time_train = time.time()\n",
    "\n",
    "# 预测  \n",
    "y_pred = clf2.predict(X_test)  \n",
    "\n",
    "# 评估模型  \n",
    "accuracy = accuracy_score(y_test, y_pred)  \n",
    "print(f'Accuracy: {accuracy:.2f}')  \n",
    "\n",
    "# 记录训练结束时间并计算用时\n",
    "end_time_train = time.time()\n",
    "train_duration = end_time_train - start_time_train\n",
    "print(f\"random_forest模型评估完成，用时：{train_duration:.2f}秒\")\n",
    "with open('clf2.pkl', 'wb') as file:\n",
    "    pickle.dump(clf2, file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logistic_regression模型训练完成，用时：12.22秒\n",
      "Accuracy: 0.54\n",
      "logistic_regression模型评估完成，用时：0.02秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\樱花玫莓\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\linear_model\\_logistic.py:469: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
      "\n",
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
      "Please also refer to the documentation for alternative solver options:\n",
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
      "  n_iter_i = _check_optimize_result(\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "clf1 = LogisticRegression(max_iter=1000,random_state=42)\n",
    "\n",
    "# 记录训练开始时间\n",
    "start_time_train = time.time()\n",
    "\n",
    "# 训练模型  \n",
    "clf1.fit(X_train, y_train)\n",
    "\n",
    "# 记录训练结束时间并计算用时\n",
    "end_time_train = time.time()\n",
    "train_duration = end_time_train - start_time_train\n",
    "print(f\"logistic_regression模型训练完成，用时：{train_duration:.2f}秒\")\n",
    "\n",
    "# 记录测试开始时间\n",
    "start_time_train = time.time()\n",
    "\n",
    "# 预测  \n",
    "y_pred = clf1.predict(X_test) \n",
    "\n",
    "# 评估模型  \n",
    "log_accuracy = accuracy_score(y_test, y_pred)  \n",
    "print(f'Accuracy: {log_accuracy:.2f}')\n",
    "\n",
    "# 记录评估结束时间并计算用时\n",
    "end_time_test = time.time()\n",
    "test_duration = end_time_test - start_time_train\n",
    "print(f\"logistic_regression模型评估完成，用时：{test_duration:.2f}秒\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\樱花玫莓\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\linear_model\\_logistic.py:469: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
      "\n",
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
      "Please also refer to the documentation for alternative solver options:\n",
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
      "  n_iter_i = _check_optimize_result(\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import VotingClassifier\n",
    "# 创建硬投票分类器\n",
    "voting_clf = VotingClassifier(estimators=[\n",
    "    ('lr', clf1), \n",
    "    ('rf', clf2), \n",
    "    ('svc', clf3)],\n",
    "    voting='soft',\n",
    "    weights=[1,1,2],\n",
    "    #n_jobs= -1\n",
    "    )\n",
    "\n",
    "# 记录训练开始时间\n",
    "start_time_train = time.time()\n",
    "\n",
    "\n",
    "# 训练集成分类器\n",
    "voting_clf.fit(X_train, y_train)\n",
    "\n",
    "#开始\n",
    "end_train_time = time.time()\n",
    "train_duration = end_train_time - start_time_train\n",
    "print(f\"hard_voting模型训练完成，用时：{train_duration:.2f}秒\")\n",
    "\n",
    "# 记录评估开始时间\n",
    "start_time_test = time.time()\n",
    "\n",
    "# 在测试集上进行预测\n",
    "predictions = voting_clf.predict(X_test)\n",
    "print(f\"Accuracy: {accuracy_score(y_test, predictions)}\")\n",
    "\n",
    "# 记录评估结束时间并计算用时\n",
    "end_time_test = time.time()\n",
    "test_duration = end_time_test - start_time_test\n",
    "print(f\"hard_voting模型评估完成，用时：{test_duration:.2f}秒\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "soft_voting模型训练完成，用时：10380.28秒\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "This 'SVC' has no attribute 'predict_proba'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\utils\\_available_if.py:29\u001b[0m, in \u001b[0;36m_AvailableIfDescriptor._check\u001b[1;34m(self, obj, owner)\u001b[0m\n\u001b[0;32m     28\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m---> 29\u001b[0m     check_result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcheck(obj)\n\u001b[0;32m     30\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\svm\\_base.py:822\u001b[0m, in \u001b[0;36mBaseSVC._check_proba\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    821\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprobability:\n\u001b[1;32m--> 822\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(\n\u001b[0;32m    823\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpredict_proba is not available when probability=False\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    824\u001b[0m     )\n\u001b[0;32m    825\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_impl \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m (\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mc_svc\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnu_svc\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n",
      "\u001b[1;31mAttributeError\u001b[0m: predict_proba is not available when probability=False",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[12], line 26\u001b[0m\n\u001b[0;32m     23\u001b[0m start_time_test \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mtime()\n\u001b[0;32m     25\u001b[0m \u001b[38;5;66;03m# 在测试集上进行预测\u001b[39;00m\n\u001b[1;32m---> 26\u001b[0m predictions \u001b[38;5;241m=\u001b[39m voting_clf\u001b[38;5;241m.\u001b[39mpredict(X_test)\n\u001b[0;32m     28\u001b[0m \u001b[38;5;66;03m# 评估集成分类器的性能\u001b[39;00m\n\u001b[0;32m     29\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAccuracy: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00maccuracy_score(y_test,\u001b[38;5;250m \u001b[39mpredictions)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\ensemble\\_voting.py:440\u001b[0m, in \u001b[0;36mVotingClassifier.predict\u001b[1;34m(self, X)\u001b[0m\n\u001b[0;32m    438\u001b[0m check_is_fitted(\u001b[38;5;28mself\u001b[39m)\n\u001b[0;32m    439\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvoting \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msoft\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m--> 440\u001b[0m     maj \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39margmax(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpredict_proba(X), axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m    442\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:  \u001b[38;5;66;03m# 'hard' voting\u001b[39;00m\n\u001b[0;32m    443\u001b[0m     predictions \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_predict(X)\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\ensemble\\_voting.py:481\u001b[0m, in \u001b[0;36mVotingClassifier.predict_proba\u001b[1;34m(self, X)\u001b[0m\n\u001b[0;32m    467\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Compute probabilities of possible outcomes for samples in X.\u001b[39;00m\n\u001b[0;32m    468\u001b[0m \n\u001b[0;32m    469\u001b[0m \u001b[38;5;124;03mParameters\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    477\u001b[0m \u001b[38;5;124;03m    Weighted average probability for each class per sample.\u001b[39;00m\n\u001b[0;32m    478\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    479\u001b[0m check_is_fitted(\u001b[38;5;28mself\u001b[39m)\n\u001b[0;32m    480\u001b[0m avg \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39maverage(\n\u001b[1;32m--> 481\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_collect_probas(X), axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m, weights\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_weights_not_none\n\u001b[0;32m    482\u001b[0m )\n\u001b[0;32m    483\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m avg\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\ensemble\\_voting.py:456\u001b[0m, in \u001b[0;36mVotingClassifier._collect_probas\u001b[1;34m(self, X)\u001b[0m\n\u001b[0;32m    454\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_collect_probas\u001b[39m(\u001b[38;5;28mself\u001b[39m, X):\n\u001b[0;32m    455\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Collect results from clf.predict calls.\"\"\"\u001b[39;00m\n\u001b[1;32m--> 456\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39masarray([clf\u001b[38;5;241m.\u001b[39mpredict_proba(X) \u001b[38;5;28;01mfor\u001b[39;00m clf \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mestimators_])\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\ensemble\\_voting.py:456\u001b[0m, in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m    454\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_collect_probas\u001b[39m(\u001b[38;5;28mself\u001b[39m, X):\n\u001b[0;32m    455\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Collect results from clf.predict calls.\"\"\"\u001b[39;00m\n\u001b[1;32m--> 456\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39masarray([clf\u001b[38;5;241m.\u001b[39mpredict_proba(X) \u001b[38;5;28;01mfor\u001b[39;00m clf \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mestimators_])\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\utils\\_available_if.py:40\u001b[0m, in \u001b[0;36m_AvailableIfDescriptor.__get__\u001b[1;34m(self, obj, owner)\u001b[0m\n\u001b[0;32m     36\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__get__\u001b[39m(\u001b[38;5;28mself\u001b[39m, obj, owner\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[0;32m     37\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m obj \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m     38\u001b[0m         \u001b[38;5;66;03m# delegate only on instances, not the classes.\u001b[39;00m\n\u001b[0;32m     39\u001b[0m         \u001b[38;5;66;03m# this is to allow access to the docstrings.\u001b[39;00m\n\u001b[1;32m---> 40\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check(obj, owner\u001b[38;5;241m=\u001b[39mowner)\n\u001b[0;32m     41\u001b[0m         out \u001b[38;5;241m=\u001b[39m MethodType(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfn, obj)\n\u001b[0;32m     43\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m     44\u001b[0m         \u001b[38;5;66;03m# This makes it possible to use the decorated method as an unbound method,\u001b[39;00m\n\u001b[0;32m     45\u001b[0m         \u001b[38;5;66;03m# for instance when monkeypatching.\u001b[39;00m\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\utils\\_available_if.py:31\u001b[0m, in \u001b[0;36m_AvailableIfDescriptor._check\u001b[1;34m(self, obj, owner)\u001b[0m\n\u001b[0;32m     29\u001b[0m     check_result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcheck(obj)\n\u001b[0;32m     30\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m---> 31\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(attr_err_msg) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n\u001b[0;32m     33\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m check_result:\n\u001b[0;32m     34\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(attr_err_msg)\n",
      "\u001b[1;31mAttributeError\u001b[0m: This 'SVC' has no attribute 'predict_proba'"
     ]
    }
   ],
   "source": [
    "#软投票\n",
    "from sklearn.ensemble import  VotingClassifier\n",
    "voting_clf = VotingClassifier(estimators=[\n",
    "    ('lr', clf1), \n",
    "    ('rf', clf2), \n",
    "    ('svc', clf3)],\n",
    "    voting='soft',\n",
    "    #n_jobs= -1\n",
    "    )\n",
    "\n",
    "# 记录训练开始时间\n",
    "start_time_train = time.time()\n",
    "\n",
    "# 训练集成分类器\n",
    "voting_clf.fit(X_train, y_train)\n",
    "\n",
    "#\n",
    "end_train_time = time.time()\n",
    "train_duration = end_train_time - start_time_train\n",
    "print(f\"soft_voting模型训练完成，用时：{train_duration:.2f}秒\")\n",
    "\n",
    "# 记录评估开始时间\n",
    "start_time_test = time.time()\n",
    "\n",
    "# 在测试集上进行预测\n",
    "predictions = voting_clf.predict(X_test)\n",
    "\n",
    "# 评估集成分类器的性能\n",
    "print(f\"Accuracy: {accuracy_score(y_test, predictions)}\")\n",
    "\n",
    "# 记录评估结束时间并计算用时\n",
    "end_time_test = time.time()\n",
    "test_duration = end_time_test - start_time_test\n",
    "print(f\"soft_voting模型评估完成，用时：{test_duration:.2f}秒\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#袋装法\n",
    "\n",
    "from sklearn.ensemble import BaggingClassifier\n",
    "bagging_clf = BaggingClassifier(base_estimator=clf1, \n",
    "    n_estimators=10, \n",
    "    random_state=42,\n",
    "    n_jobs=-1)\n",
    "\n",
    "# 记录训练开始时间\n",
    "start_time_train = time.time()\n",
    "\n",
    "#训练\n",
    "bagging_clf.fit(X_train, y_train)\n",
    "\n",
    "#结束时间\n",
    "end_train_time = time.time()\n",
    "train_duration = end_train_time - start_time_train\n",
    "print(f\"bagging模型训练完成，用时：{train_duration:.2f}秒\")\n",
    "\n",
    "# 记录评估开始时间\n",
    "start_time_test = time.time()\n",
    "\n",
    "# 在测试集上进行预测\n",
    "bag = bagging_clf.predict(X_test)\n",
    "print(f\"Accuracy: {accuracy_score(y_test, bag)}\")\n",
    "\n",
    "#结束时间\n",
    "end_train_time = time.time()\n",
    "train_duration = end_train_time - start_time_train\n",
    "print(f\"bagging模型评估完成，用时：{train_duration:.2f}秒\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#粘贴法\n",
    "from sklearn.ensemble import Baggingclassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "paste_clf = Baggingclassifier(\n",
    "    DecisionTreeClassifier(),\n",
    "    n_estimators=500,\n",
    "    max_samples=100,\n",
    "    bootstrap=False,\n",
    "    n_jobs=-1,\n",
    "    random_state=42\n",
    ")\n",
    "\n",
    "# 记录训练开始时间\n",
    "start_time_train = time.time()\n",
    "\n",
    "#训练\n",
    "paste_clf.fit(X_train, y_train)\n",
    "\n",
    "#结束时间\n",
    "end_train_time = time.time()\n",
    "train_duration = end_train_time - start_time_train\n",
    "print(f\"pasting模型训练完成，用时：{train_duration:.2f}秒\")\n",
    "\n",
    "# 记录评估开始时间\n",
    "start_time_test = time.time()\n",
    "\n",
    "# 在测试集上进行预测\n",
    "past = bagging_clf.predict(X_test)\n",
    "print(f\"Accuracy: {accuracy_score(y_test, past)}\")\n",
    "\n",
    "#结束\n",
    "end_train_time = time.time()\n",
    "train_duration = end_train_time - start_time_train\n",
    "print(f\"pasting模型评估完成，用时：{train_duration:.2f}秒\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import AdaBoostClassifier\n",
    "\n",
    "# 创建AdaBoost分类器\n",
    "ada_clf = AdaBoostClassifier(\n",
    "    DecisionTreeClassifier(max_depth=1),  # 基分类器选用决策树分类器\n",
    "    n_estimators=200,                     # 200个分类器\n",
    "    algorithm=\"SAMME.R\",                  # 使用SAMME.R算法\n",
    "    learning_rate=0.5                     # 学习率为0.5, 即每个分类器的权重缩减系数为0.5\n",
    ")\n",
    "\n",
    "# 记录训练开始时间\n",
    "start_time_train = time.time()\n",
    "\n",
    "ada_clf.fit(X_train,y_train)\n",
    "\n",
    "#结束时间\n",
    "end_train_time = time.time()\n",
    "train_duration = end_train_time - start_time_train\n",
    "print(f\"adaboost模型训练完成，用时：{train_duration:.2f}秒\")\n",
    "\n",
    "# 记录评估开始时间\n",
    "start_time_test = time.time()\n",
    "\n",
    "# 在测试集上进行预测\n",
    "past = ada_clf.predict(X_test)\n",
    "print(f\"Accuracy: {accuracy_score(y_test, past)}\")\n",
    "\n",
    "#结束\n",
    "end_train_time = time.time()\n",
    "train_duration = end_train_time - start_time_train\n",
    "print(f\"adaboost模型评估完成，用时：{train_duration:.2f}秒\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "#决策树\n",
    "\n",
    "# 创建Gradient Boosting分类器实例\n",
    "gb_clf = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, random_state=42)\n",
    "\n",
    "# 记录训练开始时间\n",
    "start_time_test = time.time()\n",
    "\n",
    "# 训练模型\n",
    "gb_clf.fit(X_train, y_train)\n",
    "\n",
    "\n",
    "#结束时间\n",
    "end_train_time = time.time()\n",
    "train_duration = end_train_time - start_time_train\n",
    "print(f\"gradient_boosting模型训练完成，用时：{train_duration:.2f}秒\")\n",
    "\n",
    "\n",
    "# 记录评估开始时间\n",
    "start_time_test = time.time()\n",
    "\n",
    "# 进行预测\n",
    "y_pred = gb_clf.predict(X_test)\n",
    "\n",
    "\n",
    "# 评估模型\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"Accuracy: {accuracy}\")\n",
    "\n",
    "#结束时间\n",
    "end_train_time = time.time()\n",
    "train_duration = end_train_time - start_time_train\n",
    "print(f\"gradient_boosting模型评估完成，用时：{train_duration:.2f}秒\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#堆叠法\n",
    "from sklearn.ensemble import StackingClassifier\n",
    "stacking_clf=StackingClassifier(\n",
    "    estimators=[\n",
    "    ('lr', clf1), \n",
    "    ('rf', clf2), \n",
    "    ('svc', clf3)],\n",
    "final_estimator=LogisticRegression(),\n",
    "cv=5,\n",
    "stack_method='auto'\n",
    ")\n",
    "\n",
    "# 记录训练开始时间\n",
    "start_time_test = time.time()\n",
    "\n",
    "stacking_clf.fit(X_train,y_train)\n",
    "\n",
    "#结束时间\n",
    "end_train_time = time.time()\n",
    "train_duration = end_train_time - start_time_train\n",
    "print(f\"stacking模型训练完成，用时：{train_duration:.2f}秒\")\n",
    "\n",
    "# 记录评估开始时间\n",
    "start_time_test = time.time()\n",
    "\n",
    "# 进行预测\n",
    "y_pred = stacking_clf.predict(X_test)\n",
    "\n",
    "# 评估模型\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"Accuracy: {accuracy}\")\n",
    "\n",
    "#结束时间\n",
    "end_train_time = time.time()\n",
    "train_duration = end_train_time - start_time_train\n",
    "print(f\"gradient_boosting模型评估完成，用时：{train_duration:.2f}秒\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
